<?php
/**
 * Notes:
 * Author: chenjie
 * @return
 * Date: 2021/11/25
 */

namespace App\Http\Controllers\Admin\Erm;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class ConfigController extends BaseController
{

    /**
     * @api {get} /erm/config/list 参数配置列表
     * @apiVersion 0.1.0
     * @apiName 参数配置列表
     * @apiGroup 系统配置
     *
     * @apiParam {Int} id id，必传
     *
     * @apiSuccessExample {json} Response-Example
     *
     *{
    "code": 200,
    "data": [
    {
    "id": 1,
    "name": "支付宝支付链接生成后有效期",
    "status": 1,
    "update_time": 1637909219,
    "admin_name": "admin"
    },
    {
    "id": 4,
    "name": "单位时间内生成待支付订单数量上限",
    "status": 1,
    "update_time": 1637909509,
    "admin_name": "admin"
    },
    {
    "id": 8,
    "name": "自动加黑：多次认证不通过",
    "status": 1,
    "update_time": 1637909686,
    "admin_name": "admin"
    }
    ],
    "message": ""
    }
     */
    public function list()
    {
        $list = app('repo_erm_config')->getConfigList();

        return Response::sendData($list);
    }


    /**
     * @api {get} /erm/config/detail 参数详情
     * @apiVersion 0.1.0
     * @apiName 参数详情
     * @apiGroup 系统配置
     *
     * @apiParam {Int} id id，必传
     *
     * @apiSuccessExample {json} Response-Example
     *
     * {
    "code": 200,
    "data": {
    "key": "ALI_PAY_LINK_EXPIRE_TIME",
    "status": 0,
    "config  不同的key返回的数据格式可能会不一样，请根据实际接收的格式单独处理": "30"
    },
    "message": ""
    }
     */
    public function detail(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:0',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $detail = app('repo_erm_config')->getConfigDetail($param['id']);

        if(!$detail)
        {
            throw new BasicException(10001, '数据不存在');
        }

        return Response::sendData($detail);
    }


    /**
     * @api {post} /erm/config/edit 编辑
     * @apiVersion 0.1.0
     * @apiName 编辑
     * @apiGroup 系统配置
     *
     * @apiParam {Int} id 必传
     * @apiParam {Int} status  状态 必传
     * @apiParam {String} config 配置 必传 具体传值格式跟后端沟通
     *
     * @apiSuccessExample {json} Response-Example
     *
     * {"code":200,"data":[]}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'status', 'config');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:0',
            'status' => 'required|integer|in:0,1',
            'config' => 'filled',

        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $param['operator_id'] = $request->admin_id ?? '';
        app('repo_erm_config')->edit($param);
        return Response::sendData([]);
    }


}